#include "System.h"
#include "Mailbox.h"

System::System()
{
}

System::~System()
{
}

void System::login()
{
    initializeLink();
    bool isExit = false;
    do
    {
        cout << "Welcome to SmartTravel Holidays Booking Management System" << endl
                << "Please login with your username and password to access related functions"
                << endl << endl;

        string user;
        cout << "Username: ";
        user.clear();
        getline(cin, user, '\n');

        string pass;
        cout << endl << "Password: ";
        pass.clear();
        getline(cin, pass, '\n');

        if (verifyLogin(user, pass))
        {
            int index = verifyLogin(user, pass) - 1;
            switch (ds.accounts[index].type)
            {
            case Client: cu.menu(ds.accounts[index].user, ds.accounts[index].ID);
                break;
            case Booking: bu.menu(ds.accounts[index].user);
                break;
            case Finance: fu.menu(ds.accounts[index].user);
                break;
            case HolidayM: hu.menu(ds.accounts[index].user, ds.accounts[index].ID);
                break;
            case General: gu.menu(ds.accounts[index].user);
                break;
            }
            ds.commit();
            isExit = true;
        }
        else
        {
            cout << endl << "Login failure. Try again." << endl << endl;
        }
    }
    while (isExit == false);
}

int System::verifyLogin(string user, string pass)
{
    int size = ds.accounts.size();
    for (int i = 0; i < size; i++)
    {
        if (user == ds.accounts[i].user)
            if (pass == ds.accounts[i].pass)
                return i + 1;
    }
    return 0;
}

void System::initializeLink()
{
    ds.populate();

    bc.link(&ds);
    hol.link(&ds, &mb);
    mg.link(&ds);
    mb.link(&ds);

    bu.link(&bc, &hol, &mg);
    cu.link(&bc, &hol, &mg, &mb);
    fu.link(&bc, &hol, &mg);
    gu.link(&bc, &hol, &mg);
    hu.link(&bc, &hol, &mg);
}